import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Request, Query } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { Platform } from './platform.entity';
import { PlatformService } from './platform.service';
import { User } from '../user/user.entity';
import { UserService } from '../user/user.service';

import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { InternetCelebrity } from '../internetCelebrity/internetCelebrity.entity';
import utils from 'src/common/utils';
import { video_SubAppId_List } from 'src/setting';
import { TypeEnum } from './platform.enum';

@Controller(apibefore('/platform'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class PlatformController {
  constructor(private readonly service: PlatformService,
    private readonly userService: UserService
    ) {}

  @Post('/create')
  async create(@Body() params: Platform, @Request() req) {
    let addItem = new Platform()
    addItem.platform = params.platform
    addItem.create_time = new Date()
    addItem.wx_user = req.headers.wx_user
    return await this.service.create(addItem)
  }

  @Get('/find_by_wx_user')
  async find_by_wx_user(@Query() params, @Request() req) {
    return await this.service.findAll_By_Wx_User(req.headers.wx_user, params.type)
  }

  @Post('/addplatform')
  async addplatform(@Body() data, @Request() req){
    let wx_user = req.headers.wx_user
    let err = await this.service.addPlatform(data.platformList, wx_user, data.ic_group)
    if(err.error){
      if(err.error && err.error.toString().indexOf('Duplicate entry') > -1 && err.error.toString().indexOf(`for key 'name'`) > -1){
        err.error = '网红已存在，请更换名字。'
      }
      return new HttpException(err.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return err.ic_group
    }
  }

  @Post('/addplatformAnswer')
  async addplatformAnswer(@Body() data, @Request() req){
    let wx_user = req.headers.wx_user
    let err = await this.service.addPlatformAnswer(data.platformList, wx_user, data.type, data.ic_group,undefined, data.addOverseas)
    if(err.error){
      return new HttpException(err.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return err.ic_group
    }
  }
  
}
